//
//  HomepageViewController.m
//  EverydayDemo
//
//  Created by qianfeng01 on 15-7-17.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "HomepageViewController.h"
#import "HomePageModel.h"
#import "HomePageView.h"
#import "DetailViewController.h"
@interface HomepageViewController ()
@property(nonatomic,strong)AFHTTPRequestOperationManager *manager;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)HomePageView *scrollView;
@end

@implementation HomepageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr=[[NSMutableArray alloc]init];
    
     self.automaticallyAdjustsScrollViewInsets = NO;
    [self downloadData];
    //[self creatTimeLabel];
    
}

-(void)creatScrollView
{
     self.scrollView=[[HomePageView alloc]initWithFrame: CGRectMake(0, 20, kScreenSize.width, kScreenSize.height-64)];
    [self.scrollView setData:self.dataArr];
    UITapGestureRecognizer *tap= [[UITapGestureRecognizer alloc] init];
    self.scrollView.showsVerticalScrollIndicator=NO;
    [tap addTarget:self action:@selector(tap:)];
    //self.scrollView.bounces=NO;
    [self.scrollView addGestureRecognizer:tap];
    [self.view addSubview:self.scrollView];
}
-(void)tap:(UITapGestureRecognizer *)tap
{
    DetailViewController *cnv=[[DetailViewController alloc]init];
       UIScrollView *view=(UIScrollView *)tap.view;
    int a=view.contentOffset.y/(kScreenSize.height-64);
    HomePageModel *model=self.dataArr[a];
    cnv.articleId=model.articleId;
    cnv.imageUrl=model.imageUrl;
    cnv.Artitle=model.title;
    cnv.categryName=model.categryName;
    cnv.authorName=model.authorName;
    cnv.readCount=model.readCount;
    cnv.url=model.url;
    cnv.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:cnv animated:YES];
}
-(void)downloadData
{
    _manager=[AFHTTPRequestOperationManager manager];
    //只下载  不解析
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    __weak typeof (self) weakSelf=self;
    [_manager GET:kHomepageUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
             NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *array=dict[@"result"];
            for (NSDictionary *dedict in array) {
                HomePageModel *model=[[HomePageModel alloc]init];
                [model setValue:dedict];
                [weakSelf.dataArr addObject:model];
            }
            [weakSelf creatScrollView];

        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
